package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.PetAdoptionInfoMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.PetAdoptionInfo;
import com.itheima.service.PetAdoptionInfoService;
import com.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PetAdoptionInfoServiceImp implements PetAdoptionInfoService {
    @Autowired
    private PetAdoptionInfoMapper petAdoptionInfoMapper;
    @Override
    public void add(PetAdoptionInfo petAdoptionInfo) {
        //补充属性值
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
//        petAdoptionInfo.setCreateUser(userId);
        petAdoptionInfoMapper.add(petAdoptionInfo);
    }

    @Override
    public PageBean<PetAdoptionInfo> list(Integer pageNum, Integer pageSize,String type, String species) {
        //1.创建PageBean对象
        PageBean<PetAdoptionInfo> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
        List<PetAdoptionInfo> as = petAdoptionInfoMapper.list(type,species);
        //Page中提供了方法，可以获取PageHelper分页查询后，得到的总记录条数和当前页数据
        Page<PetAdoptionInfo> p =(Page<PetAdoptionInfo>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userId = (Integer)map.get("id");
//        return petAdoptionInfoMapper.list(petAdoptionInfo);
    }

    @Override
    public PetAdoptionInfo findById(Integer id) {
        PetAdoptionInfo c = petAdoptionInfoMapper.findById(id);
        return c;
    }
    @Override
    public void update(PetAdoptionInfo petAdoptionInfo) {
        petAdoptionInfoMapper.update(petAdoptionInfo);

    }
    @Override
    public void updateStatus(Integer id,String status) {
        petAdoptionInfoMapper.updateStatus(id,status);

    }
    public  void delete(PetAdoptionInfo petAdoptionInfo){
        petAdoptionInfoMapper.delete(petAdoptionInfo);
    }

}
